import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class Deserialization {
    // 与序列化使用相同的文件路径
    private static final String SERIALIZE_FILE_PATH = "d:/zuo ye/chenyi_six/test.txt";

    public static void main(String[] args) {
        // 检查文件是否存在
        File file = new File(SERIALIZE_FILE_PATH);
        if (!file.exists()) {
            System.err.println("反序列化失败：文件不存在 - " + SERIALIZE_FILE_PATH);
            return;
        }

        // 使用try-with-resources自动关闭输入流
        try (FileInputStream fileInputStream = new FileInputStream(SERIALIZE_FILE_PATH);
             ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {

            // 执行反序列化操作
            Person person = (Person) objectInputStream.readObject();
            System.out.println("反序列化成功！");
            System.out.println("反序列化结果：" + person.toString());

        } catch (ClassNotFoundException e) {
            System.err.println("反序列化失败：类定义不存在 - " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("反序列化失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}